package com.authManager.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authManager.entity.SysRoleMenu;
import com.authManager.server.dao.SysRoleMenuMapper;
import com.authManager.server.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;
import com.commons.base.utils.StrUtils;

/**
 * <p>
 * 【角色菜单关联表】 服务实现类
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-16
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysRoleMenuServiceImpl.class);

	@Transactional
	@Override
	public ResultData<Object> giveRoleMenu(Long roleId,String systemCode, String menuIds, String creater) {
		baseMapper.delete(new EntityWrapper<SysRoleMenu>(new SysRoleMenu().setSysRoleId(roleId).setSystemCode(systemCode)));
		List<String> menuIdList = StrUtils.str_comma_list(menuIds);
		if (menuIdList != null && menuIdList.size()>0) {
			List<SysRoleMenu> entityList = new ArrayList<SysRoleMenu>();
			Date now = new Date();
			SysRoleMenu sysRoleMenu = null;
			for (int i = 0; i < menuIdList.size(); i++) {
				sysRoleMenu = new SysRoleMenu();
				sysRoleMenu.setSysMenuId(Long.valueOf(menuIdList.get(i)));
				sysRoleMenu.setSysRoleId(roleId);
				sysRoleMenu.setCreater(creater);
				sysRoleMenu.setSystemCode(systemCode);
				sysRoleMenu.setCreateTime(now);
				entityList.add(sysRoleMenu);
			}
			((ISysRoleMenuService) AopContext.currentProxy()).insertBatch(entityList, 300);
		}
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@Override
	public ResultData<AdminPage<SysRoleMenu>> listSysRoleMenu(Map<String, Object> paramMap) {
		Page<SysRoleMenu> page = new Page<>(Integer.valueOf(String.valueOf(paramMap.get("pageNum"))),
				Integer.valueOf(String.valueOf(paramMap.get("pageSize"))));
		Wrapper<SysRoleMenu> entityWrapper = new EntityWrapper<>();

		// 主键id
		if (paramMap.get("sysRoleMenuId") != null) {
			entityWrapper.eq("sys_role_menu.sys_role_menu_id", paramMap.get("sysRoleMenuId"));
		}
		// 角色id
		if (paramMap.get("sysRoleId") != null) {
			entityWrapper.eq("sys_role_menu.sys_role_id", paramMap.get("sysRoleId"));
		}
		// 资源id
		if (paramMap.get("sysMenuId") != null) {
			entityWrapper.eq("sys_role_menu.sys_menu_id", paramMap.get("sysMenuId"));
		}
		// 系统code
		if (paramMap.get("systemCode") != null) {
			entityWrapper.like("sys_role_menu.system_code", String.valueOf(paramMap.get("systemCode")));
		}
		// 开始-创建时间
		if (paramMap.get("start_createTime") != null) {
			entityWrapper.le("sys_role_menu.create_time", paramMap.get("start_createTime"));
		}
		// 结束-创建时间
		if (paramMap.get("end_createTime") != null) {
			entityWrapper.le("sys_role_menu.create_time", paramMap.get("end_createTime"));
		}
		this.selectPage(page, entityWrapper);
		return new ResultData.Builder<AdminPage<SysRoleMenu>>(ResultCode.success)
				.setData(new AdminPage<SysRoleMenu>(page)).build();
	}
}
